#include <stdio.h>
#include <stdlib.h>
#include <list.h>

typedef struct NODE
{
	struct list_head list;
	int data;
}	NODE;

NODE* init_list(void)
{
  NODE* pnode = malloc(sizeof(NODE));
  if (pnode != NULL)
  	INIT_LIST_HEAD(&pnode->list);//注：->的优先级要高于&
  return pnode;
}

int main(void)
{
	NODE *pos,*n;
	NODE* head;
	NODE* new;
	
	/*创建头节点*/
	head = init_list();

	/*添加节点*/
	int cnt = 3;
	while(cnt--)
	{
		new = malloc(sizeof(NODE));
		new->data = cnt;
		list_add(&new->list, &head->list);
	}

	/*遍历链表并打印数据*/
	list_for_each_entry_safe(pos, n, &head->list, list)
	{
		printf("data: %d\n",pos->data);
	}

	/*删除节点*/
	list_for_each_entry_safe(pos, n, &head->list, list)
	{
		if(pos->data == 2)
			list_del(&pos->list);
	}	

	/*遍历链表并打印数据*/
	list_for_each_entry_safe(pos, n, &head->list, list)
	{
		printf("data: %d\n",pos->data);
	}	

	/*释放链表*/
	free(head);

	return 0;
}

